<?php
namespace filemgr;

use sql\SqlExec;
use sql\SqlWhereMerge;
use model\FileInf;
use utils\PathTool;

class BizFolder
{
    var $root = array("f_id"=>"","f_nameLoc"=>"根目录","f_pid"=>"","f_pidRoot"=>"","f_pathRel"=>"");
    
    function __construct()
    {
    }   
    
    function foldersToDic($pidRoot)
    {
        //默认加载根目录
        $sql = "select f_id,f_nameLoc,f_pid,f_pidRoot from up7_folders where f_pidRoot='$pidRoot'";
        
        $se = new SqlExec();
        $folders = $se->exec_arr("up7_folders", $sql, "f_id,f_nameLoc,f_pid,f_pidRoot","");
        return $this->toDic($folders);
    }   
    
    function build_path_by_id($fdCur) 
    {
        
        $id = trim($fdCur["f_id"]);//
        $pidRoot = trim($fdCur["f_pidRoot"]);//
        
        //根目录
        $psort = array();
        if (empty($id))
        {
            $psort[]=$this->root;
            
            return json_encode($psort,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE);
        }
        
        //当前目录是子目录
        $se = new SqlExec();
        $sql = "(select f_pidRoot from up7_files where f_id='$id') union (select f_pidRoot from up7_folders where f_id='$id')";
        $data = $se->exec_arr("up7_files", $sql, "f_pidRoot","");        
        if( count($data) > 0) $pidRoot = trim($data[0]["f_pidRoot"]);
        
        //构建目录映射表(id,folder)
        $dt = $this->foldersToDic($pidRoot);
        
        //按层级顺序排列目录
        $psort = $this->sortByPid($dt, $id, $psort);
        
        
        //是子目录->添加根目录
        if (!empty($pidRoot))
        {
            $cur = $se->read("up7_files", "f_id,f_nameLoc,f_pid,f_pidRoot", array("f_id"=>$pidRoot) );
            
            array_unshift($psort,$cur);
        }//是根目录->添加根目录
        else if (!empty($id) && empty($pidRoot))
        {
            $root = $se->read("up7_files", "f_id,f_nameLoc,f_pid,f_pidRoot", array("f_id"=>$id) );
            array_unshift($psort,$root);
        }
        array_unshift($psort,$this->root);
        
        return json_encode($psort,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE);
    }
    
    function build_path($fdCur)
    {
        $pathRel = trim($fdCur["f_pathRel"]);
        $rels = array();
        $data = array("f_id"=>"","f_pid"=>"","f_pidRoot"=>"","f_nameLoc"=>"","f_pathRel"=>"");
        
        //根目录
        if($pathRel == "/")
        {
            $data["f_nameLoc"] = "根目录";
            $rels[] = $data;
            
            $data["f_pathRel"] = $pathRel;            
            $pos = strrpos($pathRel, "/");
            $data["f_nameLoc"] = substr($pathRel,$pos+1);
            array_push($rels,$data);
        }
        //子目录
        else
        {
            // /a/b/c/d/e=>,a,b,c,d,e
            $pos = 0;
            while( ($pos = strpos($pathRel,"/",$pos)) !== false )
            {
                $pathRel2 = substr($pathRel, 0, $pos);
                $nameLoc = PathTool::getName($pathRel2);
                if(empty($nameLoc)) $nameLoc = "根目录";
                if(empty($pathRel2)) $pathRel2 = "/";
                $data["f_nameLoc"] = $nameLoc;
                $data["f_pathRel"] = $pathRel2;
                array_push($rels,$data);
                $pos++;
            }
            $data["f_pathRel"] = $pathRel;
            $nameLoc = PathTool::getName($pathRel);
            $data["f_nameLoc"] = $nameLoc;
            array_push($rels,$data);
        }           
        return json_encode($rels,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE);
    }
    
    function exist_same_file($name,$pid)
    {
        $swm = new SqlWhereMerge();
        $swm->equal("f_nameLoc", $name);
        $swm->equal("f_pid", $pid);
        $swm->equal("f_deleted", 0);
        
        $sql = "select f_id from up7_files where " . $swm->to_sql();
        
        $se = new SqlExec();
        $arr = $se->exec_arr("up7_files", $sql, "f_id", "");
        return count($arr) > 0;
    }
    
    static function exist_same_folder($pathRel)
    {
        $swm = new SqlWhereMerge();
        $swm->equal("f_pathRel", $pathRel);
        $swm->equal("f_deleted", 0);
        $where = $swm->to_sql();
        
        $sql = ("(select f_id from up7_files where $where ) union (select f_id from up7_folders where $where)");
        
        $se = new SqlExec();
        $fids = $se->exec_arr("up7_files", $sql, "f_id", "");
        return count($fids) > 0;
    }
    
    static function existSameFolder($name,$pid)
    {
        $swm = new SqlWhereMerge();
        $swm->equal("f_nameLoc", $name );
        $swm->equal("f_deleted", 0);
        $swm->equal("LTRIM (f_pid)", $pid );
        $where = $swm->to_sql();
        
        $sql = ("(select f_id from up7_files where $where ) union (select f_id from up7_folders where $where)");
        
        $se = new SqlExec();
        $fids = $se->exec_arr("up7_files", $sql, "f_id", "");
        return count($fids) > 0;
    }
    
    static function getPathRel($id)
    {
        $se = new SqlExec();
        $inf = $se->read("up7_files", "f_pathRel", array("f_id"=>$id));
        if(!array_key_exists("f_pathRel", $inf))
        {
            $inf = $se->read("up7_folders", "f_pathRel", array("f_id"=>$id));
        }
        $pathRel = $inf["f_pathRel"];
        return $pathRel;
    }
    
    static function updatePathRel($pathRelOld,$pathRelNew)
    {
        $sql = "update up7_folders set f_pathRel=REPLACE(f_pathRel,'$pathRelOld/','$pathRelNew/') where locate('$pathRelOld/',f_pathRel)=1";
         
        $se = new SqlExec();
        $se->exe_sql($sql);
    }
    
    /**
     * 读取根目录或子目录信息
     * @param string $id
     * @return array 关联数组
     */
    function read($id) {
        $se = new SqlExec();
        $sql = ("(select f_pid,f_pidRoot,f_pathSvr,f_pathRel from up7_files where f_id='$id') union (select f_pid,f_pidRoot,f_pathSvr,f_pathRel from up7_folders where f_id='$id')");
        $data = $se->exec_arr("up7_files", $sql, "f_pid,f_pidRoot,f_pathSvr,f_pathRel","");
        $o = $data[0];
                
        $file = new FileInf();
        $file->id = $id;
        $file->pid = trim($o["f_pid"]);
        $file->pidRoot = trim($o["f_pidRoot"]);
        $file->pathSvr = trim($o["f_pathSvr"]);
        $file->pathRel = trim($o["f_pathRel"]);
        return $file;
    }
    
    /// <summary>
    /// 重命名文件检查
    /// </summary>
    /// <param name="newName"></param>
    /// <param name="pid"></param>
    /// <returns></returns>
    function rename_file_check($newName,$pid)
    {
        $se = new SqlExec();
        $res = $se->select("up7_files", "f_id",array("f_nameLoc"=>$newName,"f_pid"=>$pid) );
        return count($res) > 0;
    }
    
    /// <summary>
    /// 重命名目录检查
    /// </summary>
    /// <param name="newName"></param>
    /// <param name="pid"></param>
    /// <returns></returns>
    function rename_folder_check($newName, $pid)
    {
        $se = new SqlExec();
        $res = $se->select("up7_folders", "f_id", array("f_nameLoc"=>$newName,"f_pid"=>$pid) );
        return count($res) > 0;
    }
    
    function rename_file($name,$id) 
    {
        $se = new SqlExec();
        $se->update2("up7_files",array("f_nameLoc"=>$name),array("f_id"=>$id) );
    }
    
    function rename_folder($name, $id, $pid) 
    {
        $se = new SqlExec();
        $se->update2("up7_folders", array("f_nameLoc"=>$name),array("f_id"=>$id ));
    }
    
    /**
     * 添加子目录
     */
    function addChildFolder($fd)
    {
        $se = new SqlExec();
        $se->insert("up7_folders"
            , array(
                 "f_id"=>$fd->id
                ,"f_pid"=>$fd->pid
                ,"f_pidRoot"=>$fd->pidRoot
                ,"f_uid"=>$fd->uid
                ,"f_nameLoc"=>$fd->nameLoc
                ,"f_sizeLoc"=>$fd->sizeLoc
                ,"f_pathLoc"=>$fd->pathLoc
                ,"f_pathSvr"=>$fd->pathSvr
                ,"f_pathRel"=>$fd->pathRel
                ,"f_complete"=>$fd->complete
                ,"f_deleted"=>0
            ));
    }
}
?>